<template>
  <div class="task-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>任务管理</span>
          <el-button type="primary" @click="handleAdd">新增任务</el-button>
        </div>
      </template>
      
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="关键词">
          <el-input v-model="searchForm.keyword" placeholder="任务名称/区域" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="全部">
            <el-option label="全部" value="" />
            <el-option label="待处理" :value="1" />
            <el-option label="进行中" :value="2" />
            <el-option label="已完成" :value="3" />
            <el-option label="异常" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchForm.taskDate"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
      
      <el-table :data="taskList" style="width: 100%" v-loading="loading">
        <el-table-column prop="taskCode" label="任务编号" />
        <el-table-column prop="areaName" label="区域" />
        <el-table-column prop="planName" label="计划名称" />
        <el-table-column prop="priority" label="优先级">
          <template #default="{ row }">
            <el-tag :type="getPriorityType(row.priority)">
              {{ getPriorityLabel(row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="taskStatus" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.taskStatus)">
              {{ getStatusLabel(row.taskStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="taskDate" label="任务日期" />
        <el-table-column prop="cleanerName" label="负责人" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        @current-change="handlePageChange"
        layout="total, prev, pager, next"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

const loading = ref(false)
const taskList = ref([])
const searchForm = reactive({
  keyword: '',
  status: '',
  taskDate: ''
})
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

const getPriorityLabel = (priority: number) => {
  const labels = {
    1: '低',
    2: '中',
    3: '高'
  }
  return labels[priority] || '未知'
}

const getPriorityType = (priority: number) => {
  const types = {
    1: 'info',
    2: 'warning',
    3: 'danger'
  }
  return types[priority] || 'info'
}

const getStatusLabel = (status: number) => {
  const labels = {
    1: '待处理',
    2: '进行中',
    3: '已完成',
    4: '异常'
  }
  return labels[status] || '未知'
}

const getStatusType = (status: number) => {
  const types = {
    1: 'info',
    2: 'primary',
    3: 'success',
    4: 'danger'
  }
  return types[status] || 'info'
}

const loadTasks = () => {
  loading.value = true
  // 模拟数据
  taskList.value = [
    {
      id: 1,
      taskCode: 'T20240101001',
      areaName: 'A栋1单元101',
      planName: '日常保洁',
      priority: 2,
      taskStatus: 1,
      taskDate: '2024-01-01',
      cleanerName: '王师傅'
    },
    {
      id: 2,
      taskCode: 'T20240101002',
      areaName: 'B栋2单元202',
      planName: '深度清洁',
      priority: 3,
      taskStatus: 2,
      taskDate: '2024-01-01',
      cleanerName: '李阿姨'
    }
  ]
  pagination.total = 2
  loading.value = false
}

const handleSearch = () => {
  pagination.page = 1
  loadTasks()
}

const handleReset = () => {
  searchForm.keyword = ''
  searchForm.status = ''
  searchForm.taskDate = ''
  handleSearch()
}

const handlePageChange = (page: number) => {
  pagination.page = page
  loadTasks()
}

const handleAdd = () => {
  console.log('新增任务')
}

const handleEdit = (row: any) => {
  console.log('编辑任务', row)
}

const handleDelete = (row: any) => {
  console.log('删除任务', row)
}

onMounted(() => {
  loadTasks()
})
</script>

<style scoped>
.task-management {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}
</style>